<?php

/*
Category Table hack - A Wordpress hack included in the Kiwi release
*/

function cat_footer() {

$optionall = 1;
$all = 'All';
$sort_column = 'name';
$sort_order = 'asc';
$file = '';
$list = true;
$optiondates = 0;
$optioncount = 0;
$hide_empty = 0;
$use_desc_for_title = 1;
$children=FALSE;
$child_of=0;
$categories=0;
$recurse=0;
$feed = '';
$exclude = '';
$hierarchical=FALSE;
$cat_counter=0;

	global $wpdb, $category_posts;
	// Optiondates now works
	if ('' == $file) {
		$file = get_settings('home') . '/';
	}

	$exclusions = '';
	if (!empty($exclude)) {
		$excats = preg_split('/[\s,]+/',$exclude);
		if (count($excats)) {
			foreach ($excats as $excat) {
				$exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
			}
		}
	}

	if ($hide_empty) {
		$categories = array();
		$extra_fields = 'cat_name, category_nicename, category_description,';
	} else {
		if (intval($categories)==0 && !$hide_empty){
			$sort_column = 'cat_'.$sort_column;

			$query  = "
				SELECT cat_ID, cat_name, category_nicename, category_description, category_parent
				FROM $wpdb->categories
				WHERE cat_ID > 0 $exclusions
				ORDER BY $sort_column $sort_order";

			$categories = $wpdb->get_results($query);
		}
		$extra_fields = '';
	}

	if (!count($category_posts)) {
		$now = current_time('mysql', 1);
		$cat_counts = $wpdb->get_results("	SELECT cat_ID, $extra_fields
		COUNT($wpdb->post2cat.post_id) AS cat_count
		FROM $wpdb->categories 
		INNER JOIN $wpdb->post2cat ON (cat_ID = category_id)
		INNER JOIN $wpdb->posts ON (ID = post_id)
		WHERE post_status = 'publish'
		AND post_date_gmt < '$now' $exclusions
		GROUP BY category_id");
        if (! empty($cat_counts)) {
            foreach ($cat_counts as $cat_count) {
                if (1 != intval($hide_empty) || $cat_count > 0) {
                    $category_posts["$cat_count->cat_ID"] = $cat_count->cat_count;
                    if ($hide_empty) {
                    	$categories[] = $cat_count;
                    }
                }
            }
        }
	}
	
	echo '<br clear="all"/><h4>The Categories</h4>
	';
	
	$num_found=0;
	$thelist = '<div id="category-menu"><table width="100%" border="0" cellspacing="5" cellpadding="0"><tr><td><ul>
	';
	
	foreach ($categories as $category) {
		if ((intval($hide_empty) == 0 || isset($category_posts["$category->cat_ID"])) && (!$hierarchical || $category->category_parent == $child_of) ) {
			$num_found++;
			$cat_counter++;
			
			$link = '<a href="'.get_category_link($category->cat_ID).'" ';
			/* $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"'; */
			$link .= '><span>';
			$link .= apply_filters('list_cats', $category->cat_name, $category).'</span>';
			$link .= ' ' .intval($category_posts["$category->cat_ID"]).'</a>';
				
			if ($list) {
				$thelist .= "\t<li>$link\n";
			} else {
				$thelist .= "\t$link<br />\n";
			}
			if ($list) $thelist .= "</li>\n";
			}
			if (($cat_counter % 5)==0) {
				$thelist .= "</ul></td><td><ul>";
			}
	}
	$thelist .= "</ul></td></tr></table></div>";
	
	$thelist = str_replace('<ul></ul>', '', $thelist);
	$thelist = str_replace('<td></td>', '', $thelist);

	echo ($thelist);
}
?>